package cn.luoyifan.mq._02_work_queue;


import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
        public static void main(String[] args) throws IOException, TimeoutException {
            //1.创建连接
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("115.159.88.63");
            Connection connection = factory.newConnection();
            //2.创建通道
            Channel channel = connection.createChannel();

            channel.basicQos(1);

            //4.处理消息
            //消息的处理回调
            com.rabbitmq.client.Consumer callback = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("拿到消息："+ new String(body));
                    System.out.println("consumerTag = "+consumerTag);
                    System.out.println("交换机 = "+envelope.getExchange());
                    System.out.println("队列 = " +envelope.getRoutingKey());
                    System.out.println("消息的ID = "+envelope.getDeliveryTag());
                }
            };

            //3.监听队列
            channel.basicConsume(Producer.NAME_QUEUE_WORK_LUOYIFAN,//监听的队列名
                    false,//手动签收
                    callback);//消息处理回调
    }

}
